<!DOCTYPE html>
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <title>图文编辑</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link rel="shortcut icon" th:href="@{../assets/img/favicon.ico}"/>
    <link th:href="@{../assets/libs/nice-validator/dist/jquery.validator.css}" rel="stylesheet"/>
    <!-- Loading Bootstrap -->
    <link th:href="@{../assets/css/backend.css}" rel="stylesheet"/>
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements. All other JS at the end of file. -->
    <!--[if lt IE 9]>
    <script src="assets/js/html5shiv.js"></script>
    <script src="assets/js/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" th:src="@{../assets/libs/jquery/dist/jquery.js}"></script>
    <script type="text/javascript" th:src="@{../assets/libs/ckeditor/ckeditor.js}"></script>
    <script type="text/javascript" th:src="@{../assets/libs/nice-validator/dist/jquery.validator.js}"></script>
    <script type="text/javascript" th:src="@{../assets/libs/nice-validator/dist/local/zh-CN.js}"></script>
    <script type="text/javascript" th:src="@{../assets/js/login/jquery.md5.js}"></script>
    <script type="text/javascript" th:src="@{../assets/js/login/jquery.tips.js}"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url: '../system_manger/pageQuery',
                type: "get",
                dataType: "json",
                data: {"ckey": "twtype"},//图文类别
                success: function (result) {
                    var html = "";
                    $.each(result.rows, function (i, item) {
                        html += "<option value=" + item.iid + ">" + item.ctypename + "</option>";
                    })
                    $("#ctype").html(html);
                },
                error: function (error) {

                }
            })
            $.ajax({
                url: '../system_manger/pageQuery?ckey=istatus ',
                type: "get",
                dataType: "json",
                success: function (result) {
                    var html = "";
                    $.each(result.rows, function (i, item) {
                        html += "<option value=" + item.ctypename + ">" + item.ctypename + "</option>";
                    })
                    $("#istopname").html(html);
                    $("#istop").val(result.rows[0].iid);
                },
                error: function (error) {

                }
            })

            $("#istopname").change(function () {
                var role = $("#istopname").val();
                $.ajax({
                    url: '../system_manger/findOneByVO',
                    type: "post",
                    dataType: "json",
                    data: {"ctypename": role},
                    success: function (result) {
                        $("#istop").val(result.iid);
                    },
                    error: function (error) {

                    }
                })
            })
            $("#show").click(function () {
                alert(CKEDITOR.instances.editor.getData());
            });
            $("#set").click(function () {
                CKEDITOR.instances.editor.setData("<p>Pacer</p>")
            });
            // 当id为content的那个ckeditor被创建，并初始化完成之后
            CKEDITOR.instances["editor"].on("instanceReady", function () {
                // 保存按钮
                this.addCommand("save", {
                    modes: {
                        wysiwyg: 1,
                        source: 1
                    },
                    exec: function (editor) {
                        save();
                        $("#edit-form").submit();
                    }
                });
            });
            $("#edit-form")
            // 验证成功
                .on('valid.form', function () {
                    onSubmit();
                })
                // 验证失败
                .on('invalid.form', function () {
                });
        });

        function onSubmit() {
//            alert("提交");
            var formData = new FormData();
            formData.append("ctitle", $("#ctitle").val());
            formData.append("cdesc", $("#cdesc").val());
//            formData.append("articletypeiid", $("#ctype option:selected").val());
//            formData.append("articletype", $("#ctype option:selected").text());
//            formData.append("file", $("#file")[0].files[0]);
//            alert($("#file")[0].files[0].name);
            var ccontent = CKEDITOR.instances.editor.getData();
            ccontent = ccontent.replace(/\r\n/g, "")
            ccontent = ccontent.replace(/\n\r/g, "")
            ccontent = ccontent.replace(/\n/g, "");
            formData.append("ccontent", ccontent);
//            formData.append("ispassed", $("#ispassed option:selected").val());
//            formData.append("cpassed", $("#cpassed").val());
//            formData.append("istop", $("#istop").val());
//            formData.append("istopname", $("#istopname").val());
//            formData.append("isort", $("#isort").val());
            var url = $("#edit-form").attr("action");

            $.ajax({
                type: "post",
                url: url,
                async: false,
                data: formData,//表单数据
                cache: false,
                processData: false,
                contentType: false,
                success: function (d) {
                    if (d.retcode == 1) {
                        parent.layer.closeAll();//关闭弹窗
                        parent.toastr.info(d.retmsg, "操作提示");
                        // layer.msg('保存成功！',{time:1000});//保存成功提示
                        parent.$("#table").bootstrapTable('refresh');
                    }
                    if (d.retcode == 0) {
                        // layer.closeAll();//关闭弹窗
                        // layer.msg('保存异常!');
                        toastr.info(d.retmsg, "操作提示");
                    }
                },
                error: function () {

                }
            });
        }

        // 保存方法
        function save() {
            // 获取到editor中的内容
//            var content = editor.document.getBody().getHtml();
//            alert(content);
//            alert(CKEDITOR.instances.editor.getData());
        }

    </script>
</head>
<body>
<div id="main" role="main">
    <div class="tab-content tab-addtabs">
        <div id="content">
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="content">
                        <form id="edit-form" AUTOCOMPLETE="off" action="./addArticle" enctype="multipart/form-data">
                            <table width="70%" cellspacing="10">
                                <tr>
                                    <td align="right" width="60px" height="40px;">
                                        <label for="ctitle">标题：</label>
                                    </td>
                                    <td>
                                        <input class="form-control" id="ctitle" name="ctitle" data-rule="标题:required"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" height="40px;">
                                        <label for="cdesc">摘要：</label>
                                    </td>
                                    <td>
                                        <input class="form-control" id="cdesc" name="cdesc" data-rule="摘要:required"/>
                                    </td>
                                </tr>

                            </table>
                            <textarea name="editor" id="editor"></textarea>
                            <script type="text/javascript">CKEDITOR.replace('editor');</script>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" th:src="@{../assets/libs/bootstrap/dist/js/bootstrap.js}"></script>
<script type="text/javascript" th:src="@{../assets/libs/toastr/toastr.js}"></script>
<script type="text/javascript" th:src="@{../assets/libs/layer/src/layer.js}"></script>
</body>
</html>